<?php include "header.php" ?>
<body onload="getTableCouponManager(1); getPage(); getCategoryNews(0);">
<div id="wrapper">
    <?php include "main.php" ?>
    <div id="page-wrapper">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">
                            Quản lý mã khuyến mãi
                        </h1>
                        <ol class="breadcrumb">
                            <li>
                                <i class="fa fa-dashboard"></i>  <a href="<?php echo base_url() ?>admin/dashboard">Dashboard</a>
                            </li>
                            <li class="active">
                                <i class="fa fa-table"></i> <a href="<?php echo base_url() ?>admin/coupon">Quản Lý Mã Khuyến Mãi</a>
                            </li>
                            <li class="pull-right">
                                <button class="btn btn-primary btn-xs" onClick="lightbox_open('lightCreateCoupon', 'fadeCreateCoupon');"><span class="glyphicon glyphicon-plus"></span> Thêm Mới</button>
                            </li>
                        </ol>
                    </div>
                </div>
                <div class="table-responsive" id="divTableCouponManager"></div>
                <div class="row" id="pageAdminCoupon"></div>
            </div>
    </div>
</div>

<div class="row light" id="lightCreateCoupon" style="width: 70%; height: inherit; left: 24%; top: 11%;">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Thêm Mới Mã Khuyến Mãi</h3>
        </div>
        <div class="panel-body">
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3">Mã Coupon:</div>
                <div class="col-lg-8"><input type="text" class="form-control" id="txtCreateCouponCode"></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3">Tên Coupon:</div>
                <div class="col-lg-8"><input type="text" class="form-control" id="txtCreateCouponName"></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Giảm:</span></div>
                <div class="col-lg-8">
                    <div class="row">
                        <div class="col-lg-4">
                            <input id="txtCreateCouponDiscount" class="form-control" type="text" onkeypress="validate(event);">
                        </div>
                        <div class="col-lg-8">
                            <select class="form-control" id="slCreateCouponDiscount">
                                <option value="0">% với mỗi sản phẩm</option>
                                <option value="1">VND với mỗi sản phẩm</option>
                                <option value="2">VND với mỗi đơn hàng</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Điều kiện nhận Coupon (giá trị đơn hàng):</span></div>
                <div class="col-lg-8">
                    <div class="row">
                        <div class="col-lg-1">
                            <span>từ</span>
                        </div>
                        <div class="col-lg-5">
                            <input id="txtCreateCouponConditionFrom" class="form-control" type="text" onkeypress="validate(event);">
                        </div>
                        <div class="col-lg-1">
                            <span>đến</span>
                        </div>
                        <div class="col-lg-5">
                            <input id="txtCreateCouponConditionTo" class="form-control" type="text" onkeypress="validate(event);">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Ngày hết hạn:</span></div>
                <div class="col-lg-8"><input id="datetimeCreateCouponExpired" class="form-control" type="date"></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Số tiền tối thiểu của đơn hàng để áp dụng mã coupons:</span></div>
                <div class="col-lg-8"><input id="txtCreateCouponMinimumAmount" class="form-control" type="text" value="1" onkeypress="validate(event);"></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Số lần sử dụng:</span></div>
                <div class="col-lg-8"><input id="txtCreateCouponUsageTime" class="form-control" type="text" value="1" onkeypress="validate(event);"></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Trạng thái:</span></div>
                <div class="col-lg-8">
                    <select class="form-control" id="slCreateCouponStatus">
                        <option value="1">Sử dụng</option>
                        <option value="0">Ngừng sử dụng</option>
                    </select>
                </div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Áp dụng:</span></div>
                <div class="col-lg-8" style="padding-left: 30px;">
                    <div class="row">
                        <div class="input-group">
                            <input type="radio" id="radioCategoryApplyCreateCoupon" checked onclick="$('#divCategoryApplyCreateCoupon').show(); $('#divChosenCategoryApplyCreateCoupon').show(); $('#radioProductApplyCreateCoupon').prop('checked', false); $('#divProductApplyCreateCoupon').hide(); $('#divSlProductApplyCreateCoupon').hide();">
                            <label> &nbsp Áp dụng cho danh mục sản phẩm</label>
                        </div><!-- /input-group -->
                    </div>
                    <div class="row">
                        <div class="col-lg-12" id="divCategoryApplyCreateCoupon" style="height: 150px; overflow: auto; padding-top: 5px; margin-bottom: 5px;"></div>
                    </div>
                    <div class="row" style="padding: 10px;" id="divChosenCategoryApplyCreateCoupon">
                        <div class="col-lg-6">
                            <span style="text-decoration: underline">Danh mục sản phẩm đã được chọn: </span>
                        </div>
                        <div class="col-lg-6">
                            <input type="hidden" id="hiddenCreateCategoryCoupon">
                            <span style="font-weight: bold" id="divSelectedCategoryApplyCreateCoupon"></span>
                        </div>
                    </div>
                    <div class="row">
                        <div class="input-group">
                            <input type="radio" id="radioProductApplyCreateCoupon" onclick="getCategoryNews(0, 1); $('#divProductApplyCreateCoupon').show(); $('#divSlProductApplyCreateCoupon').show(); $('#radioCategoryApplyCreateCoupon').prop('checked', false); $('#divCategoryApplyCreateCoupon').hide(); $('#divChosenCategoryApplyCreateCoupon').hide();">
                            <label> &nbsp Áp dụng cho sản phẩm</label>
                        </div><!-- /input-group -->
                    </div>
                    <div class="row" id="divProductApplyCreateCoupon" style="display: none; height: 150px; overflow: auto; padding-top: 5px; margin-bottom: 5px;">
                        <div class="col-lg-12" id="divProductCategoryApplyCreateCoupon"></div>
                    </div>
                    <div class="row" id="divSlProductApplyCreateCoupon" style="display: none; margin-top: 20px;">
                        <select class="form-control" id="slProductApplyCreateCoupon" size="10" style="width: 100%; padding: 10px;" multiple></select>
                    </div>
                </div>
            </div>
            <div class="row" style="text-align: center; margin: 5px">
                <input type="button" style="margin: 5px" class="btn btn-success" value="Tạo mã khuyến mãi" onclick="createNewCoupon()">
                <input type="button" style="margin: 5px" class="btn btn-success" value="Đóng cửa sổ" onClick="lightbox_close('lightCreateCoupon', 'fadeCreateCoupon');">
                <input type="button" style="margin: 5px" class="btn btn-success" value="Xóa thông tin đã điền" onClick="emptyData();">
            </div>
        </div>
    </div>
</div>
<div class="row-fluid fade" id="fadeCreateCoupon" onClick="lightbox_close('lightCreateCoupon', 'fadeCreateCoupon');"></div>

<script>
    function getTableCouponManager(i){
        var url = "<?php echo base_url() ?>";
        $('#divTableCouponManager').load(url+"admin_coupon/getTableCouponManager/"+i);
    }

    function getCategoryNews(id, isProduct){
        var url = "<?php echo base_url() ?>";
        $.ajax({
            url: url+"admin_coupon/getAddParentCategory/"+id+"/"+isProduct,
            success: function(x){
//                alert(x);
                if(isProduct != 1){
                    $('#divCategoryApplyCreateCoupon').html(x);
                }
                else{
                    $('#divProductCategoryApplyCreateCoupon').html(x);
                }
            }
        });
    }

    function getSubCategoryNews(id, isEdit){
        var id = id;
        //alert(id);
        if(isEdit != 1){
            $('#hiddenCreateCategoryCoupon').val(id);
        }
        else{
            $('#hiddenEditCategoryCoupon').val(id);
        }
        if(id != '0'){
            var url = "<?php echo base_url() ?>";
            var dataString = "id="+id;
            $.ajax({
                type: "POST",
                url: url+"admin_category/getSubCategoryNews",
                data: dataString,
                success: function(x){
                    //alert(x);
                    if(isEdit != 1){
                        $('#divSelectedCategoryApplyCreateCoupon').html(x);
                    }
                    else{
                        $('#divSelectedCategoryApplyEditCoupon').html(x);
                    }
                }
            });
        }
        else{
            if(isEdit != 1){
                $('#divSelectedCategoryApplyCreateCoupon').html("Tất cả danh mục");
            }
            else{
                $('#divSelectedCategoryApplyEditCoupon').html("Tất cả danh mục");
            }
        }
    }

    function getCategoryProduct(idCate, isEdit){
        var idCate = idCate;
        var url = "<?php echo base_url() ?>";
        var dataString = "idCate="+idCate;
        $.ajax({
            type: "POST",
            url: url+"admin_coupon/getAddCategoryProduct",
            data: dataString,
            success: function(x){
//                alert(x);
                if(isEdit != 1){
                    $("#slProductApplyCreateCoupon option").remove();
                    var myArrResult = x.split(", ");
                    for(var k = 0; k < myArrResult.length-1; k++){
                        $("#slProductApplyCreateCoupon").append("<option style='margin: 5px;' value='"+myArrResult[k]+"'>"+myArrResult[k]+"</option>");
                    }
                }
                else{
                    $("#slProductApplyEditCoupon option").remove();
                    var myArrResult = x.split(", ");
                    for(var k = 0; k < myArrResult.length-1; k++){
                        $("#slProductApplyEditCoupon").append("<option style='margin: 5px;' value='"+myArrResult[k]+"'>"+myArrResult[k]+"</option>");
                    }
                }
            }
        });
    }
</script>

<script>
    function createNewCoupon(){
        var couponCode = $('#txtCreateCouponCode').val();
        var couponName = $('#txtCreateCouponName').val();
        var discountTotal = $('#txtCreateCouponDiscount').val();
        var discountStyle = $('#slCreateCouponDiscount').val();

        if(couponCode == '' || couponName == '' || discountTotal == ''){
            alert("Các thông đánh dấu * là các thông tin bắc buộc!");
        }
        else{
            var info = "couponCode="+couponCode+"&couponName="+couponName+"&discountTotal="+discountTotal+"&discountStyle="+discountStyle;
            if(discountStyle == '0'){
                if(discountTotal <= 0 || discountTotal > 100){
                    alert("Tổng số tiền chiết khấu phải lớn hơn 0% và nhỏ hơn hoặc bằng 100%");
                }
                else{
                    checkApplyCoupon(info);
                }
            }
            else{
                if(discountTotal < 1){
                    alert("Tổng số tiền chiết khấu phải lớn hơn hoặc bằng 1!")
                }
                else{
                    checkApplyCoupon(info);
                }
            }
        }
    }

    function checkApplyCoupon(info){
        var conditionFrom = parseFloat($('#txtCreateCouponConditionFrom').val());
        var conditionTo = parseFloat($('#txtCreateCouponConditionTo').val());
        var expiredUsage = $('#datetimeCreateCouponExpired').val();
        var minimumAmount = parseFloat($('#txtCreateCouponMinimumAmount').val());
        var usageTime = parseFloat($('#txtCreateCouponUsageTime').val());
        var couponStatus = $('#slCreateCouponStatus').val();
        var info = info;
        if(conditionFrom < 1 || $('#txtCreateCouponConditionFrom').val() == ''){
            alert("Điều kiện giá trị đơn hàng bắt đầu phải lớn hơn hoặc bằng 1!")
        }
        else if(conditionTo <= conditionFrom || $('#txtCreateCouponConditionTo').val() == ''){
            alert("Điều kiện giá trị đơn hàng kết thúc phải lớn hơn giá trị đơn hàng bắt đầu!");
        }
        else if(expiredUsage == ''){
            alert("Xin vui lòng nhập ngày hết hạn...");

        }
        else if(minimumAmount < 1 || $('#txtCreateCouponMinimumAmount').val() == ''){
            alert("Số tiền tối thiểu phải lớn hơn hoặc bằng 1!");
        }
        else if(usageTime < 1 || $('#txtCreateCouponUsageTime').val() == ''){
            alert("Số lần sử dụng phải lớn hơn hoặc bằng 1!");
        }
        else{
            if($('#radioCategoryApplyCreateCoupon').is(":checked")){
                var detailApply = $('#hiddenCreateCategoryCoupon').val();
                var detailStyle = 0;
                if(detailApply == ''){
                    alert("Xin vui lòng chọn một danh mục bất kỳ...");
                }
                else{
                    var dataString = info+"&conditionFrom="+conditionFrom+"&conditionTo="+conditionTo+"&expiredUsage="+expiredUsage+"&minimumAmount="+minimumAmount+"&usageTime="+usageTime+"&couponStatus="+couponStatus+"&detailApply="+detailApply+"&detailStyle="+detailStyle;
                    insertNewCoupon(dataString);
                }
            }
            else if($('#radioProductApplyCreateCoupon').is(":checked")){
                var detailApply = $('#slProductApplyCreateCoupon').val();
                var detailStyle = 1;
                if(detailApply == null){
                    alert("Xin vui lòng chọn ít nhất một sản phẩm...");
                }
                else{
                    var dataString = info+"&conditionFrom="+conditionFrom+"&conditionTo="+conditionTo+"&expiredUsage="+expiredUsage+"&minimumAmount="+minimumAmount+"&usageTime="+usageTime+"&couponStatus="+couponStatus+"&detailApply="+detailApply+"&detailStyle="+detailStyle;
                    insertNewCoupon(dataString);
                }
            }
        }
    }

    function insertNewCoupon(dataString){
        var url = "<?php echo base_url() ?>";
        var dataString = dataString;
        $.ajax({
            type: "POST",
            url: url+"admin_coupon/insertNewCoupon",
            data: dataString,
            success: function(x){
                if(x == 0){
                    alert("Tạo mã khuyến mãi thành công!");
                    lightbox_close('lightCreateCoupon', 'fadeCreateCoupon');
                    getTableCouponManager(1);
                    getPage();
                }
                else{
                    alert(x);
                }
            }
        });
    }

    function emptyData(){
        $('#txtCreateCouponCode').val('');
        $('#txtCreateCouponName').val('');
        $('#txtCreateCouponDiscount').val('');
        $('#slCreateCouponDiscount').val('0');
        $('#txtCreateCouponConditionFrom').val('');
        $('#txtCreateCouponConditionTo').val('');
        $('#datetimeCreateCouponExpired').val('');
        $('#txtCreateCouponMinimumAmount').val(1);
        $('#txtCreateCouponUsageTime').val(1);
        $('#slCreateCouponStatus').val('1');
        $('#hiddenCreateCategoryCoupon').val('');
        $('#divSelectedCategoryApplyCreateCoupon').html('');
        $('#slProductApplyCreateCoupon').val('');
    }
</script>

<script>
    function getPage(){
        var url = "<?php echo base_url() ?>";
        $.ajax({
            url: url+"admin_coupon/getPage",
            success: function(x){
                //alert(x);
                $('#pageAdminCoupon').html(x);
            }
        });
    }
</script>

<?php include "footer.php" ?>

</body>

</html>